import { createRouter, createWebHistory } from 'vue-router';
// import { ElMessage } from 'element-plus'
// import { getStorage } from '@/utils/localStorage';

import ComplaintView from "@/views/complaintform/ComplaintView.vue";
import HotelsView from "@/views/hotel/HotelsView.vue";
import RestaurantView from "@/views/restaurant/ReataurantView.vue";
import RoutesView from "@/views/router/RoutesView.vue";
import TrafficView from "@/views/traffic/TrafficView.vue";
import WeatherView from "@/views/weather/WeatherView.vue";
import AnnouncementView from"@/views/announcement/AnnouncementView.vue";
import EntertainmentView from "@/views/entertainment/EntertainmentView.vue";

import LoginView from '@/views/login/LoginView.vue';
import FirstView from '@/views/announcement/FirstView.vue';
import SecondView from '@/views/announcement/SecondView.vue';
import ThirdView from '@/views/announcement/ThirdView.vue';
import FourthView from '@/views/announcement/FourthView.vue';
import AnnouncementSearch from '@/views/announcementsearch/AnnouncementSearch.vue';
import {getStorage} from "@/util/localStorage";
import {ElMessage} from "element-plus";
const router =createRouter({
  history: createWebHistory(),
  routes:[
{
  path: '/',
    name:"home",
      component:
  AnnouncementView
}
,
{
  path: '/announcement',
    name:"announcement",
      component:
  AnnouncementView
}
,
{
  path: '/complaint',
    name:"complaint",
      component:
  ComplaintView,
  meta:{ requiresAuth: true }
}
,
{
  path: '/entertainment',
    name:"entertainment",
      component:
  EntertainmentView
}
,
{
  path: '/hotels',
    name:"hotels",
      component:
  HotelsView
}
,
{
  path: '/restaurant',
    name:"restaurant",
      component:
  RestaurantView
}
,
{
  path: '/routes',
    name:"routes",
      component:
  RoutesView
}
,
{
  path: '/traffic',
    name:"traffic",
      component:
  TrafficView
}
,
{
  path: '/weather',
    name:"weather",
      component:
  WeatherView
}
,
{
  path: '/loginform',
    name:"loginform",
      component:
  LoginView
}
,
{
  path: '/first',
    name: 'first',
      component:
  FirstView
},
{
  path: '/second',
    name: 'second',
      component:
  SecondView
},
{
  path: '/third',
    name: 'third',
      component:
  ThirdView
},
{
  path: '/fourth',
    name: 'fourth',
      component:
  FourthView
},
{
  path: '/announcementsearch',
    name: 'announcementsearch',
      component:
  AnnouncementSearch
}

]
});
//全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.path !== '/complaint') {
    // 如果目标路由是 /，则直接放行
    next()
  } else {
    // 否则进行身份验证、权限判断等操作
    //添加token
    let token = getStorage("Token");
    //判断有没有token
    if(token){
      next() // 放行
    } else {
      next('/loginform') // 重定向到登录页面
      ElMessage.error('投诉前请先登录')
    }
  }
})



export default router
